﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TheHeGeoCommercial.Commercial
{
    public partial class UcProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData(Request.QueryString["type"] ?? "");
            }
            else
            {
                ASPxDataView2.DataSource = Session["ucproduct_data"];
                ASPxDataView2.DataBind();
            }
        }

        protected void LoadData(string maLoaiSp)
        {
            using (var ent = new Dao.TheheGeoCommercialEntities())
            {
                var obj = (from s in ent.LoaiSanPhams
                           join l in ent.SanPhams on s.MaLoaiSanPham equals l.MaLoaiSanPham
                           where (string.IsNullOrEmpty(maLoaiSp) || s.MaLoaiSanPham.Equals(maLoaiSp))
                           select new
                           {
                               s.MaLoaiSanPham,
                               s.TenLoaiSanPham,
                               l.ImageUrl,
                               l.ChuThich,
                               l.MaSanPham,
                               l.Gia,
                               l.TenSanPham
                           });
                var dt = new DataTable();
                dt.Columns.Add("MaLoaiSanPham");
                dt.Columns.Add("TenLoaiSanPham");
                dt.Columns.Add("ImageUrl");
                dt.Columns.Add("ChuThich");
                dt.Columns.Add("MaSanPham");
                dt.Columns.Add("Gia");
                dt.Columns.Add("TenSanPham");
                dt.Columns.Add("DetailUrl");
                foreach (var v in obj)
                {
                    var dr = dt.NewRow();
                    dr["MaLoaiSanPham"] = v.MaLoaiSanPham;
                    dr["TenLoaiSanPham"] = v.TenLoaiSanPham;
                    dr["ImageUrl"] = v.ImageUrl;
                    if(v.ChuThich.Length<150)
                        dr["ChuThich"] = v.ChuThich;
                    else
                        dr["ChuThich"] = v.ChuThich.Substring(0,149) + "....";
                    dr["Gia"] = v.Gia;
                    dr["TenSanPham"] = v.TenSanPham;
                    dr["MaSanPham"] = v.MaSanPham;
                    dr["DetailUrl"] = "ProductDetail.aspx?id=" + v.MaSanPham;
                    dt.Rows.Add(dr);
                }
                Session["ucproduct_data"] = dt;
                ASPxDataView2.DataSource = dt;
                ASPxDataView2.DataBind();
            }
        }
    }
}